/*
 * DinnerRepository.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.service;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.onesalt.domain.Dinner;

/**
 * @author kiri
 * @date 2015年5月9日
 * @version 1.0
 */
public interface DinnerRepository extends PagingAndSortingRepository<Dinner, Long> {

	@Query("select d from t_dinner d where d.kitchenId = ?1 and (d.startTime < ?2 or d.status = ?3)")
	public Page<Dinner> findHistoryPagable(Long kitchenId, Date startTime, String status, Pageable page);

	@Query("select d from t_dinner d where d.kitchenId = ?1 and d.startTime > ?2 and (d.status is null or d.status <> ?3)")
	public Page<Dinner> findValidPagable(Long kitchenId, Date startTime, String status, Pageable page);

	@Query("select d from t_dinner d where d.startTime > ?1 and (d.status is null or d.status <> ?2)")
	public Page<Dinner> findAllValidPagable(Date startTime, String status, Pageable page);
	
	public List<Dinner> findByKitchenIdAndStartTimeBetween(Long kitchenId, Date startTime1, Date startTime2);
	
	public Dinner findTopByKitchenIdOrderByStartTimeDesc(Long kitchenId);
	
	@Modifying
	@Query("update t_dinner d set d.status = ?2, d.modifyTime = ?3 where d.dinnerId = ?1")
	public int setStatusFor(Long dinnerId, String status, Date modifyTime);

	@Query("select d.peopleNumber from t_dinner d where d.dinnerId = ?1")
	public int findPeopleNumberByDinnerId(Long dinnerId);
}
